<template>
  <div class="mod-menu">
    <!-- 用户列表区 -->
    <el-form size="mini" :inline="true" :model="queryMap" class="demo-form-inline">
      <el-form-item label="操作人">
        <el-input v-model="queryMap.username" placeholder="操作人" clearable></el-input>
      </el-form-item>
      <el-form-item label="ip地址">
        <el-input v-model="queryMap.ip" placeholder="ip地址" clearable></el-input>
      </el-form-item>
      <el-form-item label="操作位置">
        <el-input v-model="queryMap.location" placeholder="操作位置" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <!--TODO 查询需要优化-->
        <el-button icon="el-icon-search" @click="getLogList()" type="primary">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <template>
      <el-table
        border
        size="mini"
        :data="LogData"
        v-loading="dataListLoading"
        style="width: 100%;"
        height="460"
        @selection-change="selectionChangeHandle"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="operation" label="操作" width="150"></el-table-column>

        <el-table-column :show-overflow-tooltip="true" prop="method" label="方法" width="230">
        </el-table-column>

        <el-table-column :show-overflow-tooltip="true" prop="params" label="参数" width="200">
          <template slot-scope="scope">
            <span>{{ scope.row.params }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="time" label="耗时" width="120" sortable>
          <template slot-scope="scope">
            <el-tag v-if="scope.row.time>=2000" size="mini" type="danger">
              {{ scope.row.time + '毫秒' }}
            </el-tag>
            <el-tag size="mini" v-else-if="scope.row.time>=1000&&scope.row.time<=2000">
              {{ scope.row.time + '毫秒' }}
            </el-tag>
            <el-tag v-else type="success" size="mini">
              {{ scope.row.time + '毫秒' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="location" label="操作地点" width="240"></el-table-column>
        <el-table-column prop="ip" label="IP地址" width="160"></el-table-column>
        <el-table-column prop="username" label="操作人" width="150"></el-table-column>
        <el-table-column prop="createTime" label="时间" sortable width="180"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              icon="el-icon-delete"
              @click="deleteHandle(scope.row.id)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!--分页-->
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalCount"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data () {
    return {
      dataListLoading: false,
      // 存放登录日志数据
      LogData: [],
      // 选择的数据
      dataListSelections: [],
      totalCount: 0,
      pageIndex: 1,
      pageSize: 10,
      // 搜索参数
      queryMap: {
        username: '',
        ip: '',
        location: ''
      }
    }
  },
  created () {
    this.getLogList()
  },
  methods: {
    getLogList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sys/log/list'),
        method: 'get',
        params: this.$http.adornParams({
          'currentPage': this.pageIndex,
          'pageSize': this.pageSize,
          'username': this.queryMap.username,
          'ip': this.queryMap.ip,
          'location': this.queryMap.location
        }),
      }).then(({data}) => {
        this.totalCount = data.logPage.totalCount
        this.LogData = data.logPage.list
        this.dataListLoading = false
      })
    },
    // 删除
    deleteHandle (id) {
      let Ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${Ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/log/delete'),
          method: 'post',
          data: this.$http.adornData(Ids, false)
        }).then(({data}) => {
          if (data && data.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getLogList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getLogList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getLogList()
    },
  }
}
</script>

<style scoped>

</style>
